package com.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.mapper.DeptMapper;
import com.sys.mapper.EmployeeMapper;
import com.sys.mapper.SecurityMapper;
import com.sys.pojo.*;
import com.sys.service.SecurityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Eric Fang
 * @version 1.0
 * @date 2022/3/24 17:15
 */
@Service
public class SecurityServiceImpl extends ServiceImpl<SecurityMapper, Security> implements SecurityService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public IPage<Security> findListByPage(QueryVo queryVo) {
        IPage<Security> iPage = new Page<>(queryVo.getPage(), queryVo.getLimit());
        LambdaQueryWrapper<Security> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(queryVo.getUserName())) {
            LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(Employee::getEmployeeName, queryVo.getUserName());
            List<Employee> employees = employeeMapper.selectList(wrapper);
            List<Integer> ids = employees.stream().map(Employee::getId).collect(Collectors.toList());
            queryWrapper.in(Security::getUserId, ids);
        }
        if (queryVo.getIsFever() != null) {
            queryWrapper.eq(Security::getIsFever, queryVo.getIsFever());
        }
        IPage<Security> securityIPage = baseMapper.selectPage(iPage, queryWrapper);
        List<Security> records = securityIPage.getRecords();
        List<Security> collect = records.stream().map(security -> {
            Security security1 = new Security();
            BeanUtils.copyProperties(security,security1);
            Employee employee = employeeMapper.selectById(security.getUserId());
            Dept dept = deptMapper.selectById(security.getDeptId());
            if (employee != null) {
                security1.setUserName(employee.getEmployeeName());
            }
            if (dept != null) {
                security1.setDeptName(dept.getDeptName());
            }
            return security1;
        }).collect(Collectors.toList());
        securityIPage.setRecords(collect);
        return securityIPage;
    }

    @Override
    public Security getSecurityById(Integer id) {
        Security security = baseMapper.selectById(id);
        Employee employee = employeeMapper.selectById(security.getUserId());
        Dept dept = deptMapper.selectById(security.getDeptId());
        if (employee != null) {
            security.setUserName(employee.getEmployeeName());
        }
        if (dept != null) {
            security.setDeptName(dept.getDeptName());
        }
        return security;
    }

    @Override
    public Integer addSecurity(Security security) {
        int count = baseMapper.insert(security);
        return count;
    }

    @Override
    public Integer updateSecurity(Security security) {
        int count = baseMapper.updateById(security);
        return count;
    }

    @Override
    public Integer deleteSecurity(Integer id) {
        int i = baseMapper.deleteById(id);
        return i;
    }
}
